package Service.Impl;

import Dao.jdfwDao;
import Model.*;
import Service.kkfwService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

@Service("kkfwService")
@Transactional
public class  kkfwServiceImpl implements kkfwService {
//public class  kkfwServiceImpl implements kkfwService {
    @Autowired
    private jdfwDao dao;

    @Override
    public List<jdfw> list() {
        List<jdfw> result=new ArrayList<>();
        //得到所有的信息
        for (kfkw record:dao.list()) {
            String[] apartment=record.getKkfwApartment().split("、");
            int count =0;
            for (int i=0;i<apartment.length;i++){

                jdfw jdfws=dao.totalbyname(apartment[i]);
                if (null!=jdfws){
                    if (null!=jdfws.getTotal()){
                        count+=jdfws.getTotal();

                    }

                }
            }
            jdfw records=new jdfw();
            records.setJdfwApartment(record.getKkfwApartment());
            records.setName(record.getKkfwSection());
            records.setValue(count);
            result.add(records);
        }

        return result;
    }

    @Override
    public int eldercount() {
        return dao.eldercount();
    }

    @Override
    public int staffcount() {
        return dao.staffcount();
    }

    @Override
    public int jdfwcount() {
        return dao.jdfwcount();
    }

    @Override
    public jdfw contrast() {
        jdfw result=new jdfw();
        //获取本月的老人数量和护工数量
        //获取本年、月时间
        Calendar cale = null;
        cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        String months=""+month;
        if (month<10){
            months="0"+month;
        }
        String times=year+"-"+months+"%";
        System.out.println(times);
        //获取上个月的时间
        String lastMonths=""+(month-1);
        if ((month-1)<10){
            lastMonths="0"+(month-1);
        }
        lastMonths=year+"-"+lastMonths+"%";
        System.out.println(lastMonths);

        result.setHgThisMonthCount(dao.hgcount(times).size());
        result.setHgLastMonthCount(dao.hgcount(lastMonths).size());
        result.setLrThisMonthCount(dao.lrcount(times));
        result.setLrLastMonthCount(dao.lrcount(lastMonths));
        return result;
    }

    @Override
    public String bymonth() {
        //获取每月的老人数量
        //获取本年、月时间
        Calendar cale = null;
        cale = Calendar.getInstance();
        Integer[] result=new Integer[12];
        int year = cale.get(Calendar.YEAR);
        for (int i=1;i<=12;i++){
            String month=""+i;
            if (i<10){
                month="0"+i;
            }
            result[i-1]=dao.lrcount(year+"-"+month+"%");
        }
        return Arrays.toString(result);
    }

    @Override
    public List<kfkw> listsing(String state) {
        List<kfkw> result=new ArrayList<>();
        for (kfkw kfkw : dao.listsing(state)){
            if (null!=kfkw.gettSign()){
                kfkw.setSignResult("签到成功");
            }else{
                kfkw.setSignResult("签到失败");
            }
            result.add(kfkw);
        }
        return result;
    }

    @Override
    public List<kfkw> listStaff(String status) {
        return dao.listStaff(status);
    }

    @Override
    public List<kfkw> historyCountstatus(kfkw kfkw) {
        return dao.historyCountstatus(kfkw);
    }

//    护理机构类型
    @Override
    public List<String> listServorg(kfkw kfkw) {
        return dao.listServorg(kfkw);
    }

    //护工以及被护理老人按月存入到数据库中
    @Override
    public List<String> listStaffmonth() {
        //获得到所有的状态有效在岗的人数
        /**
         * 1，有效在岗人数
         * 2，有效待岗人数，
         * 3，无效转出人数
         */

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        String Month=year+"-"+( month < 10 ? "0" + month : month+"")+"%";

        kfkw mykfkw = new kfkw();
        List<String> servorg_list = dao.listServorg(mykfkw);
        System.out.println("servorg_list"+servorg_list);
        kfkw result=new kfkw();

        //遍历每一个机构
        for (String item:servorg_list) {

            result.setServ_org(item);
            result.setYouxiaozaigang(dao.listStaffstatus("有效在岗", item).size() + "");
            result.setMonth(Month);
            result.setNewContent("有效待岗");
            result.setYouxiaodaigang(dao.historyCount(result) + "");
            result.setNewContent("无效转出");
            result.setTime(year + "-" + (month < 10 ? "0" + month : month + ""));
            result.setHg_wuxiaozhuanchu(dao.historyCount(result) + "");
            if (dao.insertStaffCount(result) == 1) {
                System.out.println("增加月底护工各种状态人数成功");
            } else {
                System.out.println("增加月底护工各种状态人数失败");
            }

//        } //此处测试修改


//        kfkw result=new kfkw();
//        result.setYouxiaozaigang(dao.listStaff("有效在岗").size()+"");
//        Calendar cal = Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
//        int month = cal.get(Calendar.MONTH )+1;
//        String Month=year+"-"+( month<10 ? "0" + month : month+"")+"%";
//        kfkw kfkw=new kfkw();
//        kfkw.setMonth(Month);
//        kfkw.setNewContent("有效待岗");
//        result.setYouxiaodaigang(dao.historyCount(kfkw)+"");
//        kfkw.setNewContent("无效转出");
//        result.setTime(year+"-"+( month<10 ? "0" + month : month+""));
//        result.setHg_wuxiaozhuanchu(dao.historyCount(kfkw)+"");
//        if (dao.insertStaffCount(result) == 1) {
//            System.out.println("增加月底护工各种状态人数成功");
//        } else {
//            System.out.println("增加月底护工各种状态人数失败");
//        }


            //被护理老人统计
            /**
             * 获取到老人的各种人数
             * 有效正常
             * 有效暂停
             * 无效去世
             * 无效转出
             * 月服务数
             * 月累计数
             *
             */
            result.setYouxiaozhengchang(dao.eldercount() + "");
            result.setNewContent("有效暂停");
            result.setYouxiaozanting(dao.historyelderCount(result) + "");
            result.setNewContent("无效去世");
            result.setWuxiaoqushi(dao.historyelderCount(result) + "");
            result.setNewContent("无效转出");
            result.setLr_wuxiaozhuanchu(dao.historyelderCount(result) + "");

//        result.setYouxiaozhengchang(dao.eldercount()+"");
//        kfkw.setNewContent("有效暂停");
//        result.setYouxiaozanting(dao.historyelderCount(kfkw)+"");
//        kfkw.setNewContent("无效去世");
//        result.setWuxiaoqushi(dao.historyelderCount(kfkw)+"");
//        kfkw.setNewContent("无效转出");
//        result.setLr_wuxiaozhuanchu(dao.historyelderCount(kfkw)+"");

            /**
             * 得到服务数和累计数
             */
            result.setFuwushu(dao.fws(Month) + "");
            result.setLeijishu(dao.ljs(Month) + "");

            if (dao.insertElderCount(result) == 1) {
                System.out.println("增加月底老人各种状态人数成功");
            } else {
                System.out.println("增加月底老人各种状态人数失败");
            }
        }

        return null;
    }

    @Override
    public List<kfkw> listStaffmonth6() {
        List<kfkw> result=new ArrayList<>();
        for (String re:date()
                ) {
            if (dao.listStaffmonth6(re).size()!=0){
                System.out.println("dao.listStaffmonth6(re)"+dao.listStaffmonth6(re));
                result.add(dao.listStaffmonth6(re).get(0));
//                result.add(dao.listStaffmonth6(re).get(1));
            }
        }
        return result;
    }

    @Override
    public List<kfkw> listEldermonth6() {
        List<kfkw> result=new ArrayList<>();
        for (String re:date()
                ) {
            if (dao.listEldermonth6(re).size()!=0) {

                result.add(dao.listEldermonth6(re).get(0));
            }
        }
        return result;
    }

    //根据时间段查询老人的统计信息
    @Autowired
    private jdfwDao jdfwDao;
    @Override
    public List<kfkw> selectEldertime(String timeStart, String timeEnd){
        System.out.println("进入selectEldertime的service层");
        return jdfwDao.selectEldertime(timeStart,timeEnd);
    }

    //统计合计fuwushu
    @Override
    public List<ElderStatisticSum> fuwushuSum(@Param("timeStart")String timeStart, @Param("timeEnd") String timeEnd, @Param("getServorg") String getServorg){
        System.out.println("进入fuwushuSum的service层");
        return dao.fuwushuSum(timeStart,timeEnd,getServorg);
    }


    @Override
    public List<kfkw> listStaffyear(String year) {
        if (year==null||year==""||year.equals(null)||year.equals("")){
            Calendar cal = Calendar.getInstance();
            int years = cal.get(Calendar.YEAR);
            year=years+"%";
        }else{
            year=year+"%";
        }

        List<kfkw> result=dao.listStaffyear(year);
        System.out.println("sum:"+result);

        return result;
    }

    @Override
    public List<kfkw> listElderyear(String year) {
        if (year==null||year==""||year.equals(null)||year.equals("")){
            Calendar cal = Calendar.getInstance();
            int years = cal.get(Calendar.YEAR);
            year=years+"%";
        }else{
            year=year+"%";
        }
        List<kfkw> result=dao.listElderyear(year);

        return result;
    }

    //获得到最近半年的年月信息
    public static String[] date(){
        String[] result=new String[6];
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        //用于存储月份
        if (month==1){
            year-=1;
            month=12;
        }
        for (int i = 0; i< result.length; i++){
            if (month==1){
                year-=1;
                month=12;
            }else{
                month-=1;
            }
            result[i]=year+"-"+( month<10 ? "0" + month : month+"");
        }
        return result;
    }

    public static void main(String[] args) {
        for (String result:date()
                ) {
            System.out.println(result);
        }
    }

    @Override
    public List<kfkw> getServorg(){
        System.out.println("查询护理机构类型serviceImpl层");
        return dao.getServorg();
    }

    //根据年份以及机构查询护工的统计信息
    @Override
    public List<kfkw> selectByhg(@Param("yearSearch")String yearSearch, @Param("getServorg") String getServorg){
        System.out.println("进入selectEldertime的service层");
        return dao.selectByhg(yearSearch,getServorg);
    }


    //根据年份以及机构查询被护理老人的统计信息
    @Override
    public List<kfkw> selectByElder(@Param("timeStart")String timeStart, @Param("timeEnd") String timeEnd,@Param("getServorg") String getServorg){
        System.out.println("进入selectEldertime的service层");
        return dao.selectByElder(timeStart,timeEnd,getServorg);
    }

    @Override
    public List<kfkw> getAllSelectedList() {
        return dao.getAllSelectedList();
    }



}
